﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormsDemo1
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (tbAccount.Text != "" && tbPwd.Text != "")
            {
                if (rbAdmin.Checked)
                {
                    adminLogin();
                }
                else
                {
                    userLogin();
                }

            }
            else 
            {
                MessageBox.Show("用户名或密码为空");
            }
        }
        //用户登录
        public void userLogin(){
            DatabaseData db = new DatabaseData();
            string userSql = $"select id from dbo.userinfo where id={tbAccount.Text} and password ={tbPwd.Text}";
            IDataReader reader = db.read(userSql);
            if (reader.Read()) 
            {
                MessageBox.Show("登录成功");
                this.Hide();
                UserForm userForm = new UserForm();
                userForm.ShowDialog();
                this.Show();
            }
            else
            {
                MessageBox.Show("用户名或密码错误");
            }
        }
        //管理员登录
        public void adminLogin() {
            DatabaseData db = new DatabaseData();
            string userSql = $"select id from dbo.admin where id={tbAccount.Text} and password ={tbPwd.Text}";
            IDataReader reader = db.read(userSql);
            if (reader.Read())
            {
                MessageBox.Show("登录成功");
                this.Hide();
                AdminForm af = new AdminForm();
                af.ShowDialog();
                this.Show();
            }
            else 
            {
                MessageBox.Show("用户名或密码错误");
            }
        }
    }
}
